﻿using HCMS.CommonDtos;
using HCMS.Entities.Base;
using HCMS.IRepositories.Base;
using HCMS.IServices.Base;
using HCMS.Repositories.Base;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using System.Linq;

namespace HCMS.Services.Base
{
    /// <summary>
    /// 系统 字典 应用服务
    /// </summary>
    public class SystemDictionaryAppService : CommonAppService<BaseDbContext, SystemDictionary>, ISystemDictionaryAppService
    {
        private readonly IRepository<SystemDictionary> baseSystemDictionaryRepository;
        private readonly ISystemDictionaryRepository systemDictionaryRepository;

        public SystemDictionaryAppService(
            IRepository<SystemDictionary> baseSystemDictionaryRepository,
            ISystemDictionaryRepository systemDictionaryRepository
            ) : base(baseSystemDictionaryRepository)
        {
            this.baseSystemDictionaryRepository = baseSystemDictionaryRepository;
            this.systemDictionaryRepository = systemDictionaryRepository;
        }

        /// <summary>
        /// 管理 列表
        /// </summary>
        /// <param name="searchArgs">搜索 参数</param>
        /// <param name="pager">分页 参数</param>
        /// <returns></returns>
        public async Task<ListPagerDto<object>> ManageList(SearchArgsDto searchArgs, PagerArgsDto pager)
        {
            var systemDictionary = await systemDictionaryRepository.ManageList(searchArgs, pager);

            var result = new ListPagerDto<object>();
            result.PagerInfo = systemDictionary.PagerInfo;

            var data = new List<object>();
            foreach (var item in systemDictionary.Data)
            {
                data.Add(new
                {
                    id = item.Id,
                    createTime = item.CreateTime,
                    createUser = item.CreateUser,
                    updateTime = item.UpdateTime,
                    updateUser = item.UpdateUser,
                    status = item.Status,
                    title = item.Title,
                });
            }

            result.Data = data;

            return result;
        }

        /// <summary>
        /// 字典 树
        /// </summary>
        /// <returns></returns>
        public async Task<object> GetAllTree()
        {
            return await systemDictionaryRepository.GetAllTree();
        }

        /// <summary>
        /// 根据父级 Id 获取列表
        /// </summary>
        /// <param name="parentId">父级 Id</param>
        /// <returns></returns>
        public async Task<List<SystemDictionary>> ListByParentId(int parentId)
        {
            var result = (await GetAllAsync())?.Where(t => t.ParentId == parentId)?.ToList();
            return result ?? new List<SystemDictionary>();
        }

        /// <summary>
        /// 根据父级字典 Value 获取列表
        /// </summary>
        /// <remarks>
        /// 用法 例如：性别字典 value 定义为 sex，程序直接根据 sex 获取其子级的性别列表
        /// </remarks>
        /// <param name="parentValue">父级 自定义字典值</param>
        /// <returns></returns>
        public async Task<List<SystemDictionary>> ListByParentValue(string parentValue)
        {
            if (string.IsNullOrWhiteSpace(parentValue)) { return new List<SystemDictionary>(); }

            var parent = await BaseFirstOrDefaultAsync(t => t.DictionaryValue == parentValue);
            if (parent == null) { return new List<SystemDictionary>(); }

            var result = (await GetAllAsync())?.Where(t => t.ParentId == parent.Id)?.ToList();
            return result ?? new List<SystemDictionary>();
        }

        /// <summary>
        /// 不允许存在重复的 自定义字典值
        /// </summary>
        /// <param name="id">当前字典 Id</param>
        /// <param name="dictionaryValue">自定义字典值</param>
        /// <returns></returns>
        public async Task<bool> HasRepeatValue(int id, string dictionaryValue)
        {
            if (string.IsNullOrWhiteSpace(dictionaryValue)) { return false; }

            dictionaryValue = dictionaryValue.Trim();

            if (id == 0)
            {
                var result = await BaseFirstOrDefaultAsync(t => t.DictionaryValue.Trim() == dictionaryValue) != null;
                return result;
            }
            else
            {
                var result = await BaseFirstOrDefaultAsync(t => t.Id != id && t.DictionaryValue.Trim() == dictionaryValue) != null;
                return result;
            }
        }
    }
}
